package org.example.core;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;

public class View extends JFrame {

    JPanel jPanel, jPanel1, jPanel2, jPanel3;
    JButton start, clear,selectFile;
    JLabel savePath;
    JLabel transText, transKey;
    JTextField savePathField, transKeyFiled;
    JTextArea transTextField;


    public View() {
        this.jPanel = new JPanel();
        this.jPanel1 = new JPanel();
        this.jPanel2 = new JPanel();
        this.jPanel3 = new JPanel();
        this.start = new JButton("开始");
        this.clear = new JButton("清空");
        this.savePath = new JLabel("保存路径：");
        this.transText = new JLabel("翻译文本：");
        this.transKey = new JLabel("name值：");
        savePathField = new JTextField(20);
        selectFile = new JButton("选择文件");

        this.transKeyFiled = new JTextField(20);
        transTextField = new JTextArea(10, 20);
        this.setLayout(new GridLayout(4, 3));
        selectFile.setSize(20,10);

        jPanel.add(savePath);
        jPanel.add(savePathField);
        jPanel.add(selectFile);

        jPanel3.add(transKey);
        jPanel3.add(transKeyFiled);

        jPanel1.add(transText);
        jPanel1.add(transTextField);

        jPanel2.add(clear);
        jPanel2.add(start);

        this.add(jPanel);
        this.add(jPanel3);
        this.add(jPanel1);
        this.add(jPanel2);
        //给窗口设置一个标题
        this.setTitle("自动翻译软件3.0");
        //给窗口设置一个大小
        this.setSize(500, 400);
        //禁止用户改变窗口的大小
        this.setResizable(false);
        //设置窗口的初始位置
        this.setLocationRelativeTo(null);
        //this.setLocation(300, 300);
        //设置当关闭窗口时，保证JVM也关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //显示
        this.setVisible(true);
    }

    public void listenBtn() {

        selectFile.addActionListener(e->{
            JFileChooser jf = new JFileChooser();
            jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            jf.showOpenDialog(View.this);//显示打开的文件对话框
            File f =  jf.getSelectedFile();//使用文件类获取选择器选择的文件
            String s = f.getAbsolutePath();//返回路径名
            savePathField.setText(s);
        });
        savePathField.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
//                浏览文件
                savePathField.setEditable(true);
//                //设置光标的可用性为true
                savePathField.getCaret().setVisible(true);
            }
            @Override
            public void focusLost(FocusEvent e) {
                //设置文本框不可编辑后设置光标的可用性为false
                savePathField.setEditable(false);
                savePathField.getCaret().setVisible(false);
            }
        });
        clear.addActionListener(e->{
            savePathField.setText("");
            transKeyFiled.setText("");
            transTextField.setText("");
        });
    }

    public boolean isEmpty(String str) {
        if (str != null && !"".equals(str)) {
            return false;
        }
        return true;

    }


}
